<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/9
 * Time: 17:37
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

class Category extends Admin
{
    public function index()
    {
        $list = db('category')->order('sort_order desc')->select();
        $newList = categoryTree($list);

        $rows = [
//            ['id','ID'],
            ['name','分类名称'],
            ['parent_id','上级ID'],
            ['icon_url','图标','img_url'],
            ['banner_url','分类缩略图','img_url'],
            ['show_index','首页导航','switch'],
            ['right_button','操作']
        ];
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setPageTitle('分类列表')
            ->setTableName('category')
            ->addColumns($rows)
            ->addTopButton('add')
            ->addRightButtons('edit,delete')
            ->setRowList($newList)
            ->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            $data = input('post.');
            $this->store($data);
        }
        $arr = $this->getTopList();
        return ZBuilder::make('form')
            ->setPageTitle('添加分类')
            ->setUrl(url('add'))
            ->isAjax(false)
            ->addFormItems([
                ['select', 'parent_id','上级分类','',$arr,'0'],
                ['text', 'name', '分类名称'],
                ['text', 'front_desc', '分类简介'],

                ['image', 'banner_url','图标','规格1：1 小于300kb','','300'],
                ['image', 'wap_banner_url','分类缩略图','小于300kb','','300'],
                ['text','sort_order','排序','','50'],
            ])
            ->fetch();
    }

    public function edit($id = 0)
    {
        if(request()->isPost()){
            $data = input('post.');
            $this->store($data, $id);
        }
        $arr = $this->getTopList();
        $data = db('category')->where('id',$id)->find();
        return ZBuilder::make('form')
            ->setPageTitle('编辑分类')
            ->setUrl(url('edit'))
            ->addHidden('id')
            ->isAjax(false)
            ->addFormItems([
                ['select', 'parent_id','上级分类','',$arr,'0'],
                ['text', 'name', '分类名称'],
                ['text', 'front_desc', '分类简介'],

                ['image', 'banner_url','图标','规格1：1 小于300kb','','300'],
                ['image', 'wap_banner_url','分类缩略图','小于300kb','','300'],
                ['text','sort_order','排序','','50'],
            ])
            ->setFormData($data)
            ->fetch();
    }

    public function delete($ids = 0)
    {
        $res = db('category')->where('id',$ids)->delete();
        if($res){
            $this->success('删除成功','index');
        }else{
            $this->error('删除失败');
        }
    }


    private function store($data, $id = 0)
    {
//        $validate = new \app\shop\validate\GoodsCategory();
//        $check = $validate->check($data);
//        if(!$check){
//            $this->error($validate->getError());
//        }
        if($id == 0){
            $res = db('category')->insert($data);
        }else{
            $res = db('category')->where('id',$id)->update($data);
        }

        if($res){
            $this->success('保存成功','index');
        }else{
            $this->error('保存失败，请重试');
        }
    }

    private function getTopList()
    {
        $arr = db('category')->where('parent_id',0)->column('name','id');
        $arr[0] = "顶级分类";
        return $arr;
    }

}